Skip to content

fix(ghost): use well-known redirect port when scheme is unchanged - #80

Merged
perbu merged 1 commit into
mainfrom
fix/redirect-port-unchanged-scheme
Jul 28, 2026
Merged

fix(ghost): use well-known redirect port when scheme is unchanged#80
perbu merged 1 commit into
mainfrom
fix/redirect-port-unchanged-scheme

Conversation

@perbu

@perbu perbu commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Problem

build_location in ghost/src/redirect_backend.rs only substituted the redirect scheme's well-known port when the filter scheme differed from the request scheme:

} else if config.filter.scheme.is_some()
    && config.filter.scheme.as_deref() != Some(&config.original_scheme)
{

The Gateway API spec for HTTPRequestRedirectFilter.port has no such condition:

  • If redirect scheme is not-empty, the redirect port MUST be the well-known port associated with the redirect scheme. Specifically "http" to port 80 and "https" to port 443. If the redirect scheme does not have a well-known port, the listener port of the Gateway SHOULD be used.
  • If redirect scheme is empty, the redirect port MUST be the Gateway Listener port.

So on a listener bound to a non-standard port, requestRedirect: {scheme: http} with no port emitted http://example.org:8080/ where the spec wants http://example.org/.

Fix

Port resolution is now a direct reading of the spec — explicit port wins, otherwise the redirect scheme's well-known port, otherwise the listener port:

let port = if let Some(explicit_port) = config.filter.port {
    explicit_port
} else {
    match config.filter.scheme.as_deref() {
        Some("http") => 80,
        Some("https") => 443,
        _ => config.original_port,
    }
};

Two behaviour changes:

  1. Same-scheme with nil port now resolves to the well-known port (the reported bug).
  2. A redirect scheme with no well-known port falls back to the listener port. The old else { 80 } branch assigned port 80 to any non-https scheme; the spec says the listener port SHOULD be used. Not reachable today since the operator only accepts http/https, but the match arms make the rule explicit rather than incidental.

No change when the scheme genuinely differs — that path was already correct.

Why conformance did not catch this

HTTPRouteRedirectPortAndScheme never exercises the buggy branch:

  • The same-namespace-with-http-listener-on-8080 block has only scheme-nil-and-port-nil, scheme-nil-and-port-80, and scheme-https-and-port-nil.
  • scheme-http-and-port-nil appears only in the 443 block, where the scheme does change.
  • On the 80 listener the same-scheme case passed by accident, because the original port was already 80.

Added test_build_location_same_scheme_uses_well_known_port covering http-on-8080, https-on-8443, and the scheme: nil control case that must keep 8080. Its comment records why the test exists, since conformance won't flag a regression.

Testing

cargo test --lib — 78 passed. cargo clippy --lib clean.

Note: cargo fmt wants to reformat build.rs, config.rs, director.rs, and external_backend.rs — pre-existing churn on main, deliberately left out of this PR.

🤖 Generated with Claude Code

build_location only substituted the redirect scheme's well-known port when
the filter scheme differed from the request scheme. The Gateway API spec for
HTTPRequestRedirectFilter.port assigns the well-known port for any non-empty
redirect scheme, regardless of whether it matches the request.

On a listener bound to a non-standard port this produced the wrong Location:
requestRedirect {scheme: http} with no port on an http-8080 listener emitted
http://example.org:8080/ where the spec wants http://example.org/.

Also fall back to the listener port for a redirect scheme with no well-known
port, instead of defaulting it to 80.

The conformance suite cannot catch this. Its 8080-listener block covers only
scheme-nil-and-port-nil, scheme-nil-and-port-80, and scheme-https-and-port-nil;
scheme-http-and-port-nil appears only in the 443 block, where the scheme does
change. On the 80 listener the same-scheme case passed by accident because the
original port was already 80. Added a unit test for the uncovered case.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@codecov

codecov Bot commented Jul 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 71.94%. Comparing base (56937b6) to head (252fb6c).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #80      +/-   ##
==========================================
+ Coverage   71.84%   71.94%   +0.10%     
==========================================
  Files          41       41              
  Lines        6716     6716              
==========================================
+ Hits         4825     4832       +7     
+ Misses       1550     1543       -7     
  Partials      341      341              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@perbu
perbu merged commit 31580b9 into main Jul 28, 2026
5 checks passed
@perbu
perbu deleted the fix/redirect-port-unchanged-scheme branch July 28, 2026 11:10
KealanAU added a commit to KealanAU/gateway that referenced this pull request Jul 28, 2026
The merge of varnish#80 brought test_build_location_same_scheme_uses_well_known_port,
whose no-scheme case asserts exactly what test_build_location_keeps_listener_port
did; its other sub-cases repeated test_build_location_basic and
test_build_location_default_ports. The vtc keeps http-8080 and https-8443 as the
end-to-end proof that scheme and port derive from the socket name; the
default-port-omission and portless-socket clients re-ran pure functions already
unit-tested (test_should_omit_port, test_listener_port).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant